package at.guthy;

import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.AuthenticationException;

public class PicasaTableModel extends AbstractTableModel {

	private ArrayList<PhotoEntry> data = new ArrayList<PhotoEntry>();

	private String[] columnNames = { "thumbnail", "title" };

	private PicasaClient pc;

	public PicasaTableModel() {
		super();
	}

	public void reload(String albumId) {
		try {
			pc = PicasaClient.getInstance();
			this.data = pc.getPhotos(albumId);
			this.fireTableDataChanged();
		}
		catch (AuthenticationException e) {
			PicasaWindow.progressBar.setString("H");
			this.data = new ArrayList<PhotoEntry>();
			this.fireTableDataChanged();
		}

	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		PhotoEntry entry = data.get(row);
		switch (col) {
			case (0):
				try {
					return new ImageIcon(new java.net.URL(entry.getMediaThumbnails().get(0).getUrl()));
				}
				catch (MalformedURLException e) {
					e.printStackTrace();
				}
			case (1):
				return entry.getTitle().getPlainText();
		}
		return entry.getId();
	}
}